package com.net.xpay.core.manager.impl.safePay;

import com.google.common.base.Preconditions;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.common.util.LocalDateUtil;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.safePay.BookAccountChargeOrderMapper;
import com.net.xpay.core.domain.safePay.BookAccountChargeOrder;
import com.net.xpay.core.helper.EnvironmentHelper;
import com.net.xpay.core.manager.safePay.BookAccountChargeOrderManager;
import com.net.xpay.core.model.BookAccountChargeOrderSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
public class BookAccountChargeOrderManagerImpl extends AbstractVersionedManager<BookAccountChargeOrder> implements BookAccountChargeOrderManager{

    @Autowired
    private BookAccountChargeOrderMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private EnvironmentHelper environmentHelper;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(BookAccountChargeOrder obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        obj.setOutBizNo(environmentHelper.getEnv() + "-" + obj.getUserId() + "-" + obj.getId());

        return super.save(obj);
    }


    @Override
    protected BasicMapper<Long, BookAccountChargeOrder> getMapper() {
        return mapper;
    }

    @Override
    public List<Long> listNeedUpdateOrder() {
        List<Long> ids = mapper.listNeedUpdateOrder();
        return ids == null ? new ArrayList<>() : ids;
    }

    @Override
    public BookAccountChargeOrder findByImportBizNo(Long userId, String importBizNo) {
        Preconditions.checkArgument(userId != null && importBizNo != null);
        BookAccountChargeOrder condition = new BookAccountChargeOrder();
        condition.setUserId(userId);
        condition.setImportBizNo(importBizNo);
        return getByCondition(condition);
    }

    @Override
    public List<BookAccountChargeOrder> search(BookAccountChargeOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(BookAccountChargeOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public BigDecimal sumSuccessTransAmount(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);

        return mapper.sumSuccessTransAmount(userId, minEndTime, maxEndTime);
    }

    @Override
    public List<BookAccountChargeOrder> list(BookAccountChargeOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }
}
